package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.EmailModel;

import data.EmailData;

public class DeleteEmailServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = "";
		try {
			id = URLDecoder.decode(path.toLowerCase().replaceAll("/admin/email/delete/", ""),"UTF-8");
		} catch (Exception e) {
			response.sendRedirect("/admin/email/list");
		}
		try {
			EmailData emailData = new EmailData();
			EmailModel email = emailData.getEmail(id);
			emailData.delete(email);
			
			HttpSession session = request.getSession();
			session.setAttribute("success", "Bạn đã xóa email " + id + " ra khỏi hệ thống");
		} catch (Exception e) {
			// TODO: handle exception
		}
		response.sendRedirect("/admin/email/list");
	}
}
